home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / FallingBlocks / Blocks.jar / BlocksCanvas.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-03-20  |  13.1 KB  |  593 lines

  1. import java.io.IOException;
  2. import java.util.Random;
  3. import javax.microedition.lcdui.Canvas;
  4. import javax.microedition.lcdui.Font;
  5. import javax.microedition.lcdui.Graphics;
  6. import javax.microedition.lcdui.Image;
  7.  
  8. public class BlocksCanvas extends Canvas implements Runnable {
  9.    static final int MINIMUMSCROLLDELAY = 150;
  10.    static int level = 1;
  11.    Random random = new Random();
  12.    private int initTime;
  13.    private int timeleft;
  14.    private int extraTime = 0;
  15.    private int elapsedTime = 0;
  16.    private int scores = 0;
  17.    private Blocks parent;
  18.    private boolean gameOver = false;
  19.    private static final int ROWS = 7;
  20.    private static final int COLS = 7;
  21.    private static final int MIDDLE = 3;
  22.    private int[][] board = new int[7][7];
  23.    private int width;
  24.    private int height;
  25.    private int p1top;
  26.    private int p2top;
  27.    private int pleft;
  28.    private int p1height;
  29.    private int bsize = 20;
  30.    private int GAP;
  31.    private int cellW;
  32.    private int cellH;
  33.    private int bgcolor = 255;
  34.    private int blockcolor = 127;
  35.    private int pencolor = 0;
  36.    // $FF: renamed from: bg javax.microedition.lcdui.Graphics
  37.    Graphics field_0;
  38.    Image buf;
  39.    Font fcal;
  40.    int state;
  41.    int fontH;
  42.    int fontW;
  43.    int fontW2;
  44.    int waitblock = 0;
  45.    int direct = 0;
  46.    int mblock_x;
  47.    int mblock_y;
  48.    static int count = 0;
  49.    int remains;
  50.    int fallen;
  51.    private static final int maxblocks = 6;
  52.    Image[] imgs = new Image[6];
  53.    boolean[] left = new boolean[6];
  54.    Thread runner;
  55.  
  56.    public BlocksCanvas(Blocks creator) {
  57.       this.parent = creator;
  58.       this.height = ((Canvas)this).getHeight();
  59.       this.width = ((Canvas)this).getWidth();
  60.       this.buf = Image.createImage(this.width, this.height);
  61.       this.field_0 = this.buf.getGraphics();
  62.       this.fcal = Font.getFont(0, 0, 8);
  63.       this.fontH = this.fcal.getHeight();
  64.       this.fontW2 = this.fcal.stringWidth(" Game Over ");
  65.  
  66.       try {
  67.          if (this.width < 120) {
  68.             this.imgs[0] = Image.createImage("/pics/a10.png");
  69.             this.imgs[1] = Image.createImage("/pics/b10.png");
  70.             this.imgs[2] = Image.createImage("/pics/c10.png");
  71.             this.imgs[3] = Image.createImage("/pics/j10.png");
  72.             this.imgs[4] = Image.createImage("/pics/k10.png");
  73.             this.imgs[5] = Image.createImage("/pics/n10.png");
  74.             this.bsize = this.cellW = 10;
  75.             this.p1height = this.cellW + 4;
  76.             this.GAP = 1;
  77.          } else {
  78.             this.imgs[0] = Image.createImage("/pics/a20.png");
  79.             this.imgs[1] = Image.createImage("/pics/b20.png");
  80.             this.imgs[2] = Image.createImage("/pics/c20.png");
  81.             this.imgs[3] = Image.createImage("/pics/j20.png");
  82.             this.imgs[4] = Image.createImage("/pics/k20.png");
  83.             this.imgs[5] = Image.createImage("/pics/n20.png");
  84.             this.bsize = this.cellW = 20;
  85.             this.p1height = this.cellW + 4;
  86.             this.GAP = 3;
  87.          }
  88.       } catch (IOException var3) {
  89.       }
  90.  
  91.       this.p1top = 0;
  92.       this.p2top = this.p1top + this.p1height + 2 * this.GAP;
  93.       this.pleft = (this.width - this.bsize * 7 - 6 * this.GAP) / 2;
  94.       this.reset();
  95.    }
  96.  
  97.    public void reset() {
  98.       this.initTime = 99000 - (level - 1) * 2000;
  99.       if (this.initTime < 89000) {
  100.          this.initTime = 80000;
  101.       }
  102.  
  103.       this.timeleft = this.initTime;
  104.       this.state = 1;
  105.       this.direct = 0;
  106.  
  107.       for(int i = 0; i < 6; ++i) {
  108.          this.left[i] = false;
  109.       }
  110.  
  111.       this.initBoard();
  112.       this.mblock_x = 3;
  113.       this.mblock_y = 0;
  114.    }
  115.  
  116.    public void initBoard() {
  117.       for(int i = 0; i < 7; ++i) {
  118.          for(int j = 0; j < 7; ++j) {
  119.             if (i <= 2) {
  120.                this.board[i][j] = 0;
  121.             } else {
  122.                boolean cont = true;
  123.  
  124.                while(cont) {
  125.                   cont = false;
  126.                   this.board[i][j] = (this.random.nextInt() >>> 1) % 6 + 1;
  127.                   if (j != 0) {
  128.                      if (this.board[i][j] == this.board[i][j - 1] || this.board[i][j] == this.board[i - 1][j]) {
  129.                         cont = true;
  130.                      }
  131.                   } else if (this.board[i][j] == this.board[i - 1][j]) {
  132.                      cont = true;
  133.                   }
  134.                }
  135.             }
  136.          }
  137.       }
  138.  
  139.       this.board[0][3] = (this.random.nextInt() >>> 1) % 6 + 1;
  140.       this.waitblock = (this.random.nextInt() >>> 1) % 6 + 1;
  141.       this.field_0.setGrayScale(this.bgcolor);
  142.       this.field_0.fillRect(0, 0, this.width, this.height);
  143.  
  144.       for(int i = 0; i < 7; ++i) {
  145.          for(int j = 0; j < 7; ++j) {
  146.             if (this.board[i][j] != 0) {
  147.                int x = this.getLeft(j);
  148.                int y = this.getTop(i);
  149.                int ii = this.board[i][j] - 1;
  150.                this.field_0.drawImage(this.imgs[ii], x, y, 16 | 4);
  151.             }
  152.          }
  153.       }
  154.  
  155.       this.drawWaitblock(this.waitblock);
  156.       this.remains = 28;
  157.       this.fallen = 0;
  158.    }
  159.  
  160.    private int getTop(int x) {
  161.       int i = 0;
  162.       i = this.p2top + this.bsize * x + x * this.GAP;
  163.       return i;
  164.    }
  165.  
  166.    private int getLeft(int x) {
  167.       int i = 0;
  168.       if (x > 0) {
  169.          i = this.pleft + this.bsize * x + x * this.GAP;
  170.       } else {
  171.          i = this.pleft + this.bsize * x;
  172.       }
  173.  
  174.       return i;
  175.    }
  176.  
  177.    public void run() {
  178.       long nextTime = 0L;
  179.       long stopTime = 0L;
  180.       int minus_time = 0;
  181.       boolean trigger = true;
  182.       boolean xswitch = true;
  183.       if (this.gameOver) {
  184.          this.field_0.drawString("GAME OVER", this.width / 2, this.height / 2, 16 | 1);
  185.       } else {
  186.          this.paintTime();
  187.       }
  188.  
  189.       for(; !this.gameOver; ((Canvas)this).repaint()) {
  190.          long timeNow = System.currentTimeMillis();
  191.          if (trigger) {
  192.             stopTime = timeNow;
  193.             trigger = false;
  194.          }
  195.  
  196.          this.elapsedTime += 150;
  197.          this.timeleft = this.initTime - this.elapsedTime + this.extraTime;
  198.          if (nextTime < timeNow + 150L) {
  199.             nextTime = timeNow + 150L;
  200.          }
  201.  
  202.          try {
  203.             Thread.sleep(nextTime - timeNow);
  204.          } catch (Exception var20) {
  205.          }
  206.  
  207.          nextTime = System.currentTimeMillis() + 150L;
  208.          switch (this.state) {
  209.             case 1:
  210.                this.paintTime();
  211.                minus_time = Math.min(1000, this.fallen * 4);
  212.                if (this.direct != 2 && nextTime - 150L - stopTime > (long)(4000 - minus_time)) {
  213.                   if (xswitch) {
  214.                      int x = this.getLeft(this.mblock_x);
  215.                      int y = this.getTop(0);
  216.                      this.field_0.setGrayScale(this.bgcolor);
  217.                      this.field_0.fillRect(x, y, this.bsize, this.bsize);
  218.                      xswitch = false;
  219.                   } else {
  220.                      int var21 = this.getLeft(this.mblock_x);
  221.                      int var23 = this.getTop(0);
  222.                      int ii = this.board[0][this.mblock_x] - 1;
  223.                      this.field_0.drawImage(this.imgs[ii], var21, var23, 16 | 4);
  224.                      xswitch = true;
  225.                   }
  226.  
  227.                   minus_time += Math.min(1000, level * 150);
  228.                   if (nextTime - 150L - stopTime > (long)(7500 - minus_time)) {
  229.                      stopTime = nextTime;
  230.                      this.direct = 2;
  231.                   }
  232.                }
  233.  
  234.                if (this.direct != 0) {
  235.                   if (this.direct == 1) {
  236.                      int temp_x = this.mblock_x - 1;
  237.                      if (temp_x <= -1) {
  238.                         temp_x = 6;
  239.                      }
  240.  
  241.                      this.moveBlock(this.mblock_x, 0, temp_x, 0, this.board[0][this.mblock_x]);
  242.                      this.mblock_x = temp_x;
  243.                      this.direct = 0;
  244.                   } else if (this.direct == 2) {
  245.                      stopTime = 0L;
  246.                      int temp_y = this.mblock_y + 1;
  247.                      if (temp_y <= 6 && this.board[temp_y][this.mblock_x] == 0) {
  248.                         this.moveBlock(this.mblock_x, this.mblock_y, this.mblock_x, temp_y, this.board[this.mblock_y][this.mblock_x]);
  249.                         this.mblock_y = temp_y;
  250.                      } else {
  251.                         ++this.remains;
  252.                         ++this.fallen;
  253.                         this.scores += 10;
  254.                         this.findconnect(this.mblock_x, this.mblock_y, this.board[this.mblock_y][this.mblock_x]);
  255.                         if (count >= 2) {
  256.                            this.remove();
  257.                            this.paintTime();
  258.                            this.rearrange();
  259.                            this.reverse();
  260.                            this.extraTime += (count - 1) * 2000;
  261.                            this.scores += (count - 1) * 20;
  262.  
  263.                            for(int i = 6; i >= 1; --i) {
  264.                               for(int j = 0; j < 7; ++j) {
  265.                                  count = 0;
  266.                                  if (this.board[i][j] != 0) {
  267.                                     this.findconnect(j, i, this.board[i][j]);
  268.                                     if (count >= 2) {
  269.                                        this.remove();
  270.                                        this.rearrange();
  271.                                        this.reverse();
  272.                                        this.extraTime += (count - 1) * 2000;
  273.                                        this.scores += (count - 1) * 20;
  274.                                        count = 0;
  275.                                        break;
  276.                                     }
  277.  
  278.                                     this.reverse();
  279.                                  }
  280.                               }
  281.                            }
  282.                         }
  283.  
  284.                         this.reverse();
  285.                         this.paintTime();
  286.                         if (!this.isGameover()) {
  287.                            if (this.fallen % 32 == 0) {
  288.                               this.raiseBlocks();
  289.                            }
  290.  
  291.                            for(int i = 0; i < 7; ++i) {
  292.                               if (this.board[0][i] != 0) {
  293.                                  this.state = 4;
  294.                               }
  295.                            }
  296.  
  297.                            if (this.state != 4) {
  298.                               int temp = (this.random.nextInt() >>> 1) % 6 + 1;
  299.                               if (this.remains < 15) {
  300.                                  for(int i = 0; i < 6; ++i) {
  301.                                     this.left[i] = false;
  302.                                  }
  303.  
  304.                                  for(int i = 1; i < 7; ++i) {
  305.                                     for(int j = 0; j < 7; ++j) {
  306.                                        if (this.board[i][j] == 1) {
  307.                                           this.left[0] = true;
  308.                                        } else if (this.board[i][j] == 2) {
  309.                                           this.left[1] = true;
  310.                                        } else if (this.board[i][j] == 3) {
  311.                                           this.left[2] = true;
  312.                                        } else if (this.board[i][j] == 4) {
  313.                                           this.left[3] = true;
  314.                                        } else if (this.board[i][j] == 5) {
  315.                                           this.left[4] = true;
  316.                                        } else if (this.board[i][j] == 6) {
  317.                                           this.left[5] = true;
  318.                                        }
  319.                                     }
  320.                                  }
  321.  
  322.                                  if (this.left[0] || this.left[1] || this.left[2] || this.left[3] || this.left[4] || this.left[5]) {
  323.                                     while(!this.left[temp - 1]) {
  324.                                        temp = (this.random.nextInt() >>> 1) % 6 + 1;
  325.                                     }
  326.                                  }
  327.                               }
  328.  
  329.                               this.board[0][3] = this.waitblock;
  330.                               int var22 = this.getLeft(3);
  331.                               int var24 = this.getTop(0);
  332.                               int var26 = this.board[0][3] - 1;
  333.                               this.field_0.drawImage(this.imgs[var26], var22, var24, 16 | 4);
  334.                               this.mblock_x = 3;
  335.                               this.mblock_y = 0;
  336.                               this.waitblock = temp;
  337.                               this.drawWaitblock(temp);
  338.                            }
  339.                         }
  340.  
  341.                         count = 0;
  342.                         this.direct = 0;
  343.                         trigger = true;
  344.                      }
  345.                   } else if (this.direct == 3) {
  346.                      int var25 = this.mblock_x + 1;
  347.                      if (var25 >= 7) {
  348.                         var25 = 0;
  349.                      }
  350.  
  351.                      this.moveBlock(this.mblock_x, 0, var25, 0, this.board[0][this.mblock_x]);
  352.                      this.mblock_x = var25;
  353.                      this.direct = 0;
  354.                   }
  355.                }
  356.  
  357.                if (this.timeleft <= 900) {
  358.                   this.state = 4;
  359.                }
  360.  
  361.                if (this.remains == 0) {
  362.                   this.state = 5;
  363.                }
  364.             case 2:
  365.             case 3:
  366.             default:
  367.                break;
  368.             case 4:
  369.                this.gameover();
  370.                this.endGame();
  371.                break;
  372.             case 5:
  373.                this.youwin();
  374.                this.endGame();
  375.          }
  376.       }
  377.  
  378.    }
  379.  
  380.    public void paint(Graphics g) {
  381.       g.drawImage(this.buf, 0, 0, 16 | 4);
  382.    }
  383.  
  384.    private void paintTime() {
  385.       int temp = this.timeleft / 1000;
  386.       this.field_0.setGrayScale(this.bgcolor);
  387.       this.field_0.fillRect(0, 0, this.width - this.cellW - 2 * this.GAP, this.p1height);
  388.       this.field_0.setGrayScale(this.pencolor);
  389.       this.field_0.drawString("T: " + temp + " S: " + this.scores + " L:" + level, 1, 1, 16 | 4);
  390.    }
  391.  
  392.    private void drawWaitblock(int val) {
  393.       this.field_0.drawImage(this.imgs[val - 1], this.width - this.cellW - 2 * this.GAP, 2, 16 | 4);
  394.    }
  395.  
  396.    private void moveBlock(int from_x, int from_y, int to_x, int to_y, int num) {
  397.       this.clearBlock(from_x, from_y);
  398.       int x = this.getLeft(to_x);
  399.       int y = this.getTop(to_y);
  400.       this.board[to_y][to_x] = num;
  401.       int ii = this.board[to_y][to_x] - 1;
  402.       this.field_0.drawImage(this.imgs[ii], x, y, 16 | 4);
  403.    }
  404.  
  405.    private void findconnect(int cx, int cy, int value) {
  406.       this.board[cy][cx] = value * -1;
  407.       if (cx < 6 && cy >= 0 && cy <= 7 && this.board[cy][cx + 1] == value) {
  408.          ++count;
  409.          this.findconnect(cx + 1, cy, value);
  410.       }
  411.  
  412.       if (cx > 0 && cy >= 0 && cy <= 7 && this.board[cy][cx - 1] == value) {
  413.          ++count;
  414.          this.findconnect(cx - 1, cy, value);
  415.       }
  416.  
  417.       if (cx >= 0 && cx <= 6 && cy < 6 && this.board[cy + 1][cx] == value) {
  418.          ++count;
  419.          this.findconnect(cx, cy + 1, value);
  420.       }
  421.  
  422.       if (cx >= 0 && cx <= 6 && cy > 0 && this.board[cy - 1][cx] == value) {
  423.          ++count;
  424.          this.findconnect(cx, cy - 1, value);
  425.       }
  426.  
  427.    }
  428.  
  429.    private void reverse() {
  430.       for(int i = 0; i < 7; ++i) {
  431.          for(int j = 0; j < 7; ++j) {
  432.             if (this.board[i][j] < 0) {
  433.                this.board[i][j] *= -1;
  434.             }
  435.          }
  436.       }
  437.  
  438.    }
  439.  
  440.    private void remove() {
  441.       for(int i = 0; i < 7; ++i) {
  442.          for(int j = 0; j < 7; ++j) {
  443.             if (this.board[i][j] < 0) {
  444.                this.board[i][j] = 0;
  445.                this.clearBlock(j, i);
  446.                --this.remains;
  447.             }
  448.          }
  449.       }
  450.  
  451.    }
  452.  
  453.    private void rearrange() {
  454.       boolean b = true;
  455.  
  456.       while(b) {
  457.          b = false;
  458.  
  459.          for(int i = 5; i >= 1; --i) {
  460.             for(int j = 0; j < 7; ++j) {
  461.                if (this.board[i + 1][j] == 0 && this.board[i][j] != 0) {
  462.                   this.moveBlock(j, i, j, i + 1, this.board[i][j]);
  463.                   b = true;
  464.                }
  465.             }
  466.          }
  467.       }
  468.  
  469.    }
  470.  
  471.    private void clearBlock(int xpos, int ypos) {
  472.       int x = this.getLeft(xpos);
  473.       int y = this.getTop(ypos);
  474.       this.board[ypos][xpos] = 0;
  475.       this.field_0.setGrayScale(this.bgcolor);
  476.       this.field_0.fillRect(x, y, this.bsize, this.bsize);
  477.    }
  478.  
  479.    private void raiseBlocks() {
  480.       int temp = 1;
  481.  
  482.       for(int i = 1; i < 7; ++i) {
  483.          for(int j = 0; j < 7; ++j) {
  484.             if (this.board[i][j] != 0) {
  485.                this.moveBlock(j, i, j, i - 1, this.board[i][j]);
  486.             }
  487.          }
  488.       }
  489.  
  490.       for(int j = 0; j < 7; ++j) {
  491.          boolean cont = true;
  492.  
  493.          while(cont) {
  494.             cont = false;
  495.             temp = (this.random.nextInt() >>> 1) % 6 + 1;
  496.             if (j != 0) {
  497.                if (temp == this.board[6][j - 1] || temp == this.board[5][j]) {
  498.                   cont = true;
  499.                }
  500.             } else if (temp == this.board[5][j]) {
  501.                cont = true;
  502.             }
  503.          }
  504.  
  505.          this.board[6][j] = temp;
  506.          int x = this.getLeft(j);
  507.          int y = this.getTop(6);
  508.          this.field_0.drawImage(this.imgs[temp - 1], x, y, 16 | 4);
  509.       }
  510.  
  511.       this.remains += 7;
  512.    }
  513.  
  514.    private boolean isGameover() {
  515.       return this.gameOver;
  516.    }
  517.  
  518.    private void gameover() {
  519.       this.field_0.setGrayScale(this.bgcolor);
  520.       this.field_0.fillRect(this.width / 2 - this.fontW2 / 2, this.height / 2, this.fontW2, this.fontH);
  521.       this.field_0.setGrayScale(this.pencolor);
  522.       this.field_0.drawString("Game Over", this.width / 2, this.height / 2, 16 | 1);
  523.    }
  524.  
  525.    private void youwin() {
  526.       this.field_0.setGrayScale(this.bgcolor);
  527.       this.field_0.fillRect(this.width / 2 - this.fontW2 / 2, this.height / 2, this.fontW2, this.fontH);
  528.       this.field_0.setGrayScale(this.pencolor);
  529.       this.field_0.drawString("You Win", this.width / 2, this.height / 2, 16 | 1);
  530.    }
  531.  
  532.    public void endGame() {
  533.       this.runner = null;
  534.       this.gameOver = true;
  535.    }
  536.  
  537.    void restartGame() {
  538.       this.timeleft = this.initTime;
  539.       if (this.state == 5) {
  540.          ++level;
  541.       } else {
  542.          level = 1;
  543.       }
  544.  
  545.       this.extraTime = this.elapsedTime = count = this.scores = 0;
  546.       this.field_0.setGrayScale(this.bgcolor);
  547.       this.field_0.fillRect(0, 0, this.width, this.height);
  548.       this.reset();
  549.       if (this.gameOver) {
  550.          this.gameOver = false;
  551.          this.start();
  552.       }
  553.  
  554.       this.gameOver = false;
  555.    }
  556.  
  557.    synchronized void start() {
  558.       this.runner = new Thread(this);
  559.       this.runner.start();
  560.    }
  561.  
  562.    synchronized void pause() {
  563.    }
  564.  
  565.    void dest() {
  566.       this.parent.destroyApp(true);
  567.    }
  568.  
  569.    public void keyRepeated(int keyCode) {
  570.       this.keyPressed(keyCode);
  571.    }
  572.  
  573.    public void keyPressed(int keyCode) {
  574.       if (this.direct == 0) {
  575.          int action = ((Canvas)this).getGameAction(keyCode);
  576.          switch (action) {
  577.             case 2:
  578.                this.direct = 1;
  579.             case 3:
  580.             case 4:
  581.             default:
  582.                break;
  583.             case 5:
  584.                this.direct = 3;
  585.                break;
  586.             case 6:
  587.                this.direct = 2;
  588.          }
  589.       }
  590.  
  591.    }
  592. }
  593.